package OSdesign.GUI_show;

import redis.clients.jedis.Jedis;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class GUI_login extends JFrame{

        JPanel panel;
        JPanel panel_login;
        JPanel panel_rigister;
        JLabel label; // #标签# 大标题
        JTextField textField;
        JPasswordField passwordField;

        public JFrame getJFrame(){
            return this;
        }
        public GUI_login() {
            //layeredPane = new JLayeredPane();
            panel_login = new JPanel();
            panel_rigister = new JPanel();
            panel = new JPanel();
            panel.setBounds(400, 200, 1000,700);
            panel_login.setBounds(0, 0, 1000,700);
            panel_rigister.setBounds(0, 0, 1000,700);
            panel_login.setLayout(null);
            panel_rigister.setLayout(null);
            panel.setLayout(null);
            Font font = new Font("", Font.BOLD, 20);

            //登录部分组件
            //ImageIcon icon = new ImageIcon("C:\\Users\\24135\\Pictures\\Saved Pictures\\xuexiao1.jpeg");
            label = new JLabel("田径运动会管理系统");
            label.setBounds(240,-50,800,300);
            label.setForeground(Color.BLACK);
            label.setFont(new Font("",Font.BOLD,55));

            JLabel label1 = new JLabel("用户名:");
            label1.setBounds(290,200,70,50);
            //label1.setForeground(Color.black);
            //label1.setBackground(Color.WHITE);
            //label1.setOpaque(true);
            label1.setFont(font);
            JLabel label2 = new JLabel("密 码:");
            label2.setBounds(290,300,70,50);
            label2.setForeground(Color.black);
            //label2.setBackground(Color.WHITE);
            //label2.setOpaque(true);
            label2.setFont(font);
            textField = new JTextField();
            passwordField = new JPasswordField();
            textField.setBounds(370,200,300,50);
            textField.setFont(font);
            passwordField.setBounds(370,300,300,50);
            passwordField.setFont(font);

            JButton[] button = new JButton[4];
            button[0] = new JButton("登录");
            button[0].setBounds(370,400,300,50);
            button[0].setBackground(Color.green);
            button[0].setFont(font);
            button[0].addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    Jedis jedis =  new Jedis();
                    String user = textField.getText();
                    String passwd = jedis.get(textField.getText());
                    //通过user键  获取passwd值 然后和文本框输入的作比较
                    if(passwd.equals(passwordField.getText())){
                        JOptionPane.showMessageDialog(null,"登陆成功！");
                        new Menu().mainMenu();
                        getJFrame().dispose();
                    }else{
                        JOptionPane.showMessageDialog(null,"用户名或密码错误！");
                    }
                }
            });

            //#标签#  还没有账户
            JLabel jl = new JLabel("还没有账户？");
            jl.setBounds(370,500,240,50);
            jl.setForeground(Color.blue);
            jl.setFont(font);

            button[1] = new JButton("注册");
            button[1].setBounds(500,500,100,50);
            button[1].setBackground(Color.cyan);
            button[1].addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    panel.removeAll();
                    panel.add(panel_rigister);
                    panel.validate();
                    panel.repaint();
                }
            });

            //  注册部分 组件
            JLabel r_label = new JLabel("田径运动会管理系统-注册");
            r_label.setBounds(170,-50,800,300);
            r_label.setForeground(Color.BLACK);
            r_label.setFont(new Font("",Font.BOLD,55));

            JLabel r_label1 = new JLabel("用户名:");
            r_label1.setBounds(290,200,70,50);
            r_label1.setFont(font);
            JLabel r_label2 = new JLabel("密 码:");
            r_label2.setBounds(290,270,70,50);
            r_label2.setFont(font);
            JLabel r_label3 = new JLabel("确认密码:");
            r_label3.setBounds(270,340,100,50);
            r_label3.setFont(font);
            JTextField r_textField = new JTextField();
            JPasswordField r_passwordField = new JPasswordField();
            JPasswordField r_passwordFields = new JPasswordField();
            r_textField.setBounds(370,200,300,50);
            r_textField.setFont(font);
            r_passwordField.setBounds(370,270,300,50);
            r_passwordField.setFont(font);
            r_passwordFields.setBounds(370,340,300,50);
            r_passwordFields.setFont(font);

            button[2] = new JButton("注册");
            button[2].setBounds(370,450,300,50);
            button[2].setBackground(Color.pink);
            button[2].setFont(font);
            button[2].addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    Jedis jedis = new Jedis();
                    String user = r_textField.getText();
                    String passwd = r_passwordField.getText();
                    String passwds = r_passwordFields.getText();
                    if(passwd.equals(passwds)){
                        jedis.set(user,passwd);
                        JOptionPane.showMessageDialog(null,"注册成功！");
                        panel.removeAll();
                        panel.add(panel_login);
                        panel.validate();
                        panel.repaint();
                    }else{
                        JOptionPane.showMessageDialog(null,"密码不一致，请重新输入！");
                    }
                }
            });
            button[3] = new JButton("返回登录");
            button[3].setBounds(830,600,150,50);
            button[3].setBackground(Color.pink);
            button[3].setFont(font);
            button[3].addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    panel.removeAll();
                    panel.add(panel_login);
                    panel.validate();
                    panel.repaint();
                }
            });

            //  登录容器界面  组件添加
            panel_login.add(label);
            panel_login.add(label1);
            panel_login.add(label2);
            panel_login.add(textField);
            panel_login.add(passwordField);
            panel_login.add(jl);
            panel_login.add(button[0]);
            panel_login.add(button[1]);
            panel.add(panel_login);

            //  注册容器界面  组件添加
            panel_rigister.add(r_label);
            panel_rigister.add(r_label1);
            panel_rigister.add(r_label2);
            panel_rigister.add(r_label3);
            panel_rigister.add(r_textField);
            panel_rigister.add(r_passwordField);
            panel_rigister.add(r_passwordFields);
            panel_rigister.add(button[2]);
            panel_rigister.add(button[3]);
            //panel.add(panel_rigister);

            this.setBounds(400, 200, 1000,700);
            this.add(panel);
            this.setVisible(true);
            this.setResizable(false);
            this.setTitle("田径运动会管理系统");
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }

}
